package mapEditor.view
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.assetEdit.AssetPanelView;
	import mapEditor.view.resItemEdit.ResItemPanelView;
	import mapEditor.vo.ModuleType;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;

	public class LeftPanelView extends Sprite
	{
		/**
		 *当前面板 
		 */		
		private var _panelView:DisplayObject;
		
		public function LeftPanelView()
		{
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			drawFrame();
			setPanel();
		}
		
		private function addEvent():void
		{
			StageReflection.stage.addEventListener(Event.RESIZE, __onStageResize);
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
		}
		
		private function removeEvent():void
		{
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStageResize);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
		}
		
		private function __onResModuleTypeChange(evt:MapEditorEvent):void
		{
			setPanel();
		}
		
		private function __onSceneModuleTypeChange(evt:MapEditorEvent):void
		{
			setPanel();
		}		
		
		private function setPanel():void
		{
			DisplayUtil.dispose(_panelView);
			_panelView=null;
			
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.resModuleType)
					{
						case ModuleType.RES_ASSET_EDIT:
							_panelView=new AssetPanelView();
							addChild(_panelView);
							break;
						case ModuleType.RES_ITEM_EDIT:
							_panelView=new ResItemPanelView();
							addChild(_panelView);
							break;
					}
					break;
				case ModuleType.SCENE_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.sceneModuleType)
					{
						case ModuleType.SCENE_TERRAIN_EDIT:
							break;
					}
					break;
			}
		}
		
		private function __onStageResize(evt:Event):void
		{
			drawFrame();
		}
		
		/**
		 *绘制窗体边框 
		 * 
		 */
		private function drawFrame():void
		{
			graphics.clear();
			graphics.beginFill(0xd6d6d6);
			graphics.drawRect(0, 0, 160, StageReflection.stageHeight-90-2);
			graphics.lineStyle(1, 0x3a3a3a);
			graphics.moveTo(160, 0);
			graphics.lineTo(160, StageReflection.stageHeight-90-2);
			graphics.endFill();
		}
		
		public function dispose():void
		{
			removeEvent();
			graphics.clear();
			DisplayUtil.dispose(_panelView);
			_panelView=null;
			if(parent)parent.removeChild(this);
		}
	}
}